﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Noftware.Com.Cryptography;

namespace TestCryptographer
{
    /// <summary>
    /// Test class for testing encryption and decryption.
    /// </summary>
    [TestClass]
    public class TestCryptography
    {
        private const string TextSalt = "12345ABC";
        private const int NumericSalt = 12345678;

        private const string EncryptedXor = "愚愦愧愽慮愧愽慮愺愫愶愺慮愺愡慮愬愫慮愫愠愭愼愷愾愺愫愪";
        private const string EncryptedHex = "54686973206973207465787420746F20626520656E63727970746564";
        private const string EncryptedMd5 = "pzHWw4yDQvEmx3SWLBNLL0pXBaVoTMYL26eYF89wDIU=";
        private const string EncryptedRijndael = "NkL7tRcB8LP/ELXTV/WhkPyXVGWxz1uOqAaX6azJm9axwkBn92dmaMjQ0POfRGoVsaIw99FIhAbmQY8BUtYsnQ==";

        private const string DecryptedText = "This is text to be encrypted";

        [TestMethod]
        public void TestEncryptionXor()
        {
            var settings = new CryptographySettings()
            {
                CryptographyType = CryptographyType.Xor,
                Input = DecryptedText,
                Mode = Mode.Encryption,
                NumericKey = NumericSalt
            };
            var gen = new Generator();
            string result = gen.Generate(settings);

            Assert.AreEqual(result, EncryptedXor);
        }

        [TestMethod]
        public void TestDecryptionXor()
        {
            var settings = new CryptographySettings()
            {
                CryptographyType = CryptographyType.Xor,
                Input = EncryptedXor,
                Mode = Mode.Decryption,
                NumericKey = NumericSalt
            };
            var gen = new Generator();
            string result = gen.Generate(settings);

            Assert.AreEqual(result, DecryptedText);
        }

        [TestMethod]
        public void TestEncryptionHex()
        {
            var settings = new CryptographySettings()
            {
                CryptographyType = CryptographyType.Hex,
                Input = DecryptedText,
                Mode = Mode.Encryption
            };
            var gen = new Generator();
            string result = gen.Generate(settings);

            Assert.AreEqual(result, EncryptedHex, true);
        }

        [TestMethod]
        public void TestDecryptionHex()
        {
            var settings = new CryptographySettings()
            {
                CryptographyType = CryptographyType.Hex,
                Input = EncryptedHex,
                Mode = Mode.Decryption
            };
            var gen = new Generator();
            string result = gen.Generate(settings);

            Assert.AreEqual(result, DecryptedText);
        }

        [TestMethod]
        public void TestEncryptionMd5()
        {
            var settings = new CryptographySettings()
            {
                CryptographyType = CryptographyType.Md5,
                Input = DecryptedText,
                Mode = Mode.Encryption,
                StringKey = TextSalt
            };
            var gen = new Generator();
            string result = gen.Generate(settings);

            Assert.AreEqual(result, EncryptedMd5);
        }

        [TestMethod]
        public void TestDecryptionMd5()
        {
            var settings = new CryptographySettings()
            {
                CryptographyType = CryptographyType.Md5,
                Input = EncryptedMd5,
                Mode = Mode.Decryption,
                StringKey = TextSalt
            };
            var gen = new Generator();
            string result = gen.Generate(settings);

            Assert.AreEqual(result, DecryptedText);
        }

        [TestMethod]
        public void TestEncryptionRijndael()
        {
            var settings = new CryptographySettings()
            {
                CryptographyType = CryptographyType.Rijndael,
                Input = DecryptedText,
                Mode = Mode.Encryption,
                StringKey = TextSalt
            };
            var gen = new Generator();
            string result = gen.Generate(settings);

            Assert.AreEqual(result, EncryptedRijndael);
        }

        [TestMethod]
        public void TestDecryptionRijndael()
        {
            var settings = new CryptographySettings()
            {
                CryptographyType = CryptographyType.Rijndael,
                Input = EncryptedRijndael,
                Mode = Mode.Decryption,
                StringKey = TextSalt
            };
            var gen = new Generator();
            string result = gen.Generate(settings);

            Assert.AreEqual(result, DecryptedText);
        }
    }
}
